﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectWebNhac.DAO;
using System.Data;
using ProjectWebNhac.DTO;

namespace ProjectWebNhac.BUS
{
    public class CaSiBus
    {
        Dataprovider dao = new Dataprovider();
        DataTable dt;
        TheLoaiBus theloaiBus = new TheLoaiBus();
        CasiTheloaiBus casitheloaiBus = new CasiTheloaiBus();

        public DataTable LayDanhSachCaSi()
        {
            dt = dao.LayDuLieuThamSo("sp_Admin_LayDanhSachCaSi", CommandType.StoredProcedure);
            return dt;
        }

        public DataTable LayChiTietCasi(int MaCasi)
        {
            return dao.LayDuLieuThamSo("sp_Admin_LayChitietCasi", CommandType.StoredProcedure,
                new object[] { "@MaCasi", MaCasi });
        }

        public DataTable LayChiTietCasiTheoBaiHat(int MaBaiHat)
        {
            return dao.LayDuLieuThamSo("sp_User_LayChiTietCaSiTheoBaiHat", CommandType.StoredProcedure,
                new object[] { "@MaBaiHat", MaBaiHat });
        }
        
        public DataTable LayChiTietCasiTheoAlbum(int MaAlbum)
        {
            return dao.LayDuLieuThamSo("sp_User_LayChiTietCaSiTheoAlbum", CommandType.StoredProcedure,
                new object[] { "@MaAlbum", MaAlbum });
        }



        public bool ThemCasi(CaSiDTO objCasi)
        {
            bool isSuccess = false;
            int MaCasi = 0;
            isSuccess = dao.XuLySuKien("sp_Admin_ThemCaSi", CommandType.StoredProcedure,
                new object[] {"@Ten",objCasi.TenCasi,"@NgaySinh",objCasi.NgaySinh,
                "@CtDaiDien",objCasi.CongtyDaiDien,"@MaQuocGia",objCasi.MaQuocGia,
                "@ThongTinChiTiet",objCasi.ThongTinCaSi,"@HinhAnh",objCasi.HinhAnh });
            if (isSuccess)
            {
                MaCasi = Convert.ToInt16(dao.LayDuLieuThamSo("sp_Admin_LayMaCaSi",
                    CommandType.StoredProcedure).Rows[0][0]);
            }
            foreach (var matheloai in theloaiBus.LayMaTheLoaiTheoTen(objCasi.Theloai))
            {
                isSuccess = casitheloaiBus.ThemCasiTheLoai(MaCasi, matheloai);
            }
            return isSuccess;
        }

        public bool CapNhatThongTin(CaSiDTO objCasi)
        {
            bool isSuccess = false;
            isSuccess = dao.XuLySuKien("sp_Admin_CapNhatThongtinCs", CommandType.StoredProcedure,
                new object[] {"@MaCaSi",objCasi.MaCasi,"@Ten",objCasi.TenCasi,"@NgaySinh",objCasi.NgaySinh,
                "@CongTyDaiDien",objCasi.CongtyDaiDien,"@MaQuocGia",objCasi.MaQuocGia,"@ThongTinChiTiet",objCasi.ThongTinCaSi});
            isSuccess = casitheloaiBus.XoaTheLoaiCaSiTheoMaCS(objCasi.MaCasi);
            foreach (var matheloai in theloaiBus.LayMaTheLoaiTheoTen(objCasi.Theloai))
            {
                isSuccess = casitheloaiBus.ThemCasiTheLoai(objCasi.MaCasi, matheloai);
            }
            return isSuccess;
        }

        public List<int> LayMaCasiTheoTen(string TenCaSi)
        {
            List<string> lsTenCaSi = TenCaSi.Split(',').ToList();

            List<int> lsMaCs = new List<int>();

            foreach (var tencs in lsTenCaSi)
            {
                string newTenCs = tencs.TrimStart();
                newTenCs = newTenCs.TrimEnd();
                DataTable dt = dao.LayDuLieuThamSo("sp_Admin_LayMaCasiTheoTen",
                CommandType.StoredProcedure, new object[] { "@TenCasi", newTenCs });

                if (dt.Rows.Count > 0)
                {
                    lsMaCs.Add(Convert.ToInt16(dt.Rows[0][0]));
                }
                else
                {
                    CaSiDTO objCasi = new CaSiDTO();
                    objCasi.TenCasi = tencs;
                    objCasi.NgaySinh = "Chưa cập nhật";
                    objCasi.CongtyDaiDien = "Chưa cập nhật";
                    objCasi.MaQuocGia = 1;
                    objCasi.ThongTinCaSi = "Chưa cập nhật";
                    objCasi.HinhAnh = "male-avatar-icon-614x460.png";
                    
                    ThemCasi(objCasi);
                    lsMaCs.Add(Convert.ToInt16(dao.LayDuLieuThamSo("sp_Admin_LayMaCasiTheoTen",
                        CommandType.StoredProcedure,
                        new object[] { "@TenCasi", newTenCs }).Rows[0][0]));
                }
            }

            return lsMaCs;
        }

        public string LayCaSiTheoBaiHat(int MaBaiHat)
        {
            DataTable dt = dao.LayDuLieuThamSo("sp_Admin_LayCaSiTheoBaiHat", CommandType.StoredProcedure,
                new object[] { "@MaBaiHat", MaBaiHat });
            string CaSi = "";
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    CaSi += row["TenCaSi"].ToString() + " ,";
                }
                CaSi = CaSi.Remove(CaSi.Length - 1);
            }
            else
                CaSi = "Đang Cập Nhật";
            return CaSi;
        }


        public string LayCaSiTheoVideo(int MaVideo)
        {
            DataTable dt = dao.LayDuLieuThamSo("sp_User_LayCaSiTheoVideo", CommandType.StoredProcedure,
                new object[] { "@MaVideo", MaVideo });
            string CaSi = "";
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    CaSi += row["TenCaSi"].ToString() + " ,";
                }
                CaSi = CaSi.Remove(CaSi.Length - 1);
            }
            else
                CaSi = "Đang Cập Nhật";
            return CaSi;
        }

        public DataTable LayCaSiTheoBaiHatTable(int MaBaiHat)
        {
            DataTable dt = dao.LayDuLieuThamSo("sp_User_LayCaSiTheoBaiHat", CommandType.StoredProcedure,
                new object[] { "@MaBaiHat", MaBaiHat });
            return dt;
        }

        public DataTable LayCaSiTheoVideoTable(int MaVideo)
        {
            DataTable dt = dao.LayDuLieuThamSo("sp_User_LayCaSiTheoVideo", CommandType.StoredProcedure,
                new object[] { "@MaVideo", MaVideo });
            return dt;
        }


        public DataTable LayDanhSachCaSiVietNamTop15()
        {
            return dao.LayDuLieuThamSo("sp_User_LayDanhSachCaSiVietNamTop15", CommandType.StoredProcedure);
        }

        public DataTable LayDanhSachCaSiHanQuocTop15()
        {
            return dao.LayDuLieuThamSo("sp_User_LayDanhSachCaSiHanQuocTop15", CommandType.StoredProcedure);
        }

        public DataTable LayDanhSachCaSiAuMyTop15()
        {
            return dao.LayDuLieuThamSo("sp_User_LayDanhSachCaSiAuMyTop15", CommandType.StoredProcedure);
        }

        public DataSet LayDsDanhSachCaSi(int currPage,
        int recodperpage,
        int Pagesize,
        int MaTheLoai)
        {
            return dao.LayDsThamSo("sp_User_PhanTrangCaSi", CommandType.StoredProcedure,
               new object[] { "@currPage", currPage, "@recodperpage", recodperpage, "Pagesize",
                    Pagesize,"@MaTheLoai",MaTheLoai });
        }

        public DataSet TimKiemTheoTenCasi(int currPage, int recodperpage, int Pagesize, string keyword)
        {
            return dao.LayDsThamSo("sp_User_TimKiemTheoTenCasi", CommandType.StoredProcedure,
                new object[] { "@currPage", currPage, "@recodperpage", recodperpage, "Pagesize",
                    Pagesize,"@txtSearch",keyword });
        }
    }
}